package Util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.LinkedList;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Resources;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.widget.TableLayout;
import android.widget.Toast;

public class Util {
	private static String key_path = "LAST_PATH";
	private static String extension = "MYDEP.dep";
	private static String spliter = "\\|";
	private static String writer_spliter = "|";

	public static void setupTableParams(TableLayout.LayoutParams tableRowParams) {

	}

	public static int dipToPixel(Context context, int dip) {
		return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
				dip, context.getResources().getDisplayMetrics());
	}
	//using this function
	public static int convertDpToPixel(float dp, Context context) {
		Resources resources = context.getResources();
		DisplayMetrics metrics = resources.getDisplayMetrics();
		float px = dp * (metrics.densityDpi / 160f);
		return (int) px;
	}

	public static void insertLastPath(Context context, String value) {
		if (TextUtils.isEmpty(value)) {
			return;
		}
		SharedPreferences preferences = PreferenceManager
				.getDefaultSharedPreferences(context);
		Editor edit = preferences.edit();
		edit.putString(key_path, value);
		edit.commit();
		Toast.makeText(context, "saved " + value, Toast.LENGTH_SHORT).show();
	}

	public static String GetLastPath(Context context) {
		SharedPreferences preferences = PreferenceManager
				.getDefaultSharedPreferences(context);
		Toast.makeText(context,
				"last path  " + preferences.getString(key_path, null),
				Toast.LENGTH_SHORT).show();
		return preferences.getString(key_path, null);
	}

	public static boolean writeToFile(Context context, String filename,
			String path, int[] colA, int curMaxIndex) {
		Log.e("writeToFile", "writing");
		// write on SD card file data in the text box
		boolean result = false;
		filename = extension;
		try {
			File myFile = new File(path + "/" + filename);
			myFile.createNewFile();
			FileOutputStream fOut = new FileOutputStream(myFile);
			OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
			for (int i = 0; i < curMaxIndex; i++) {
				Log.e("writeToFile", "append " + colA[i]);
				myOutWriter.append(colA[i] + writer_spliter);
			}
			myOutWriter.close();
			fOut.close();
			result = true;
			Toast.makeText(context, "Done writing SD " + filename,
					Toast.LENGTH_SHORT).show();
		} catch (Exception e) {
			Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
		}
		return result;
	}

	public static void readFromFile(Context context, int[] colA, int curMaxIndex) {
		Log.e("readFromFile", "reading");
		try {
			String path = GetLastPath(context);
			if (TextUtils.isEmpty(path)) {
				return;
			}
			File myFile = new File(path + "/" + extension);
			FileInputStream fIn = new FileInputStream(myFile);
			BufferedReader myReader = new BufferedReader(new InputStreamReader(
					fIn));
			String aDataRow = "";
			while ((aDataRow = myReader.readLine()) != null) {
				Log.e("readFromFile", "line = " + aDataRow);
				String arr[] = aDataRow.split(spliter);
				for (int i = 0; i < arr.length; i++) {
					if (!TextUtils.isEmpty(arr[i])) {
						colA[i] = Integer.parseInt(arr[i]);
						curMaxIndex = i + 1;
					}
					Log.e("arr = " + arr[i], "curMax = " + curMaxIndex);
				}
			}
			myReader.close();
			return;
		} catch (Exception e) {
			Log.e("problem", e.getMessage());
			return;
		}
	}

	public static LinkedList<String> visitAllFiles(File dir) {
		LinkedList<String> fileList = new LinkedList<String>();
		if (dir.isDirectory()) {
			String[] children = dir.list();
			if (dir.canRead()) {
				for (int i = 0; i < children.length; i++) {
					visitAllFiles(new File(dir, children[i]));
				}
			}
		} else {
			String ap = dir.getAbsolutePath();
			if (ap.endsWith(".dep")) {
				fileList.add(ap);
			}
		}
		return fileList;
	}

	public static String numToColumn(int i) {
		boolean go = true;
		String t = "";
		while (go) {
			int x = i % 26;
			String s = "";
			switch (x) {
			case 0:
				s = "Rd";
				break;
			case 1:
				s = "Blk";
				break;
			case 2:
				s = "1xS";
				break;
			case 3:
				s = "2xS";
				break;
			case 4:
				s = "3xS";
				break;
			case 5:
				s = "BBL";
				break;
			case 6:
				s = "FTL";
				break;
			case 7:
				s = "Ard";
				break;
			case 8:
				s = "Abk";
				break;

			case 9:
				s = "Evn";
				break;
			case 10:
				s = "Odd";
				break;
			case 11:
				s = "1xS";
				break;
			case 12:
				s = "2xS";
				break;
			case 13:
				s = "3xS";
				break;
			case 14:
				s = "BBL";
				break;
			case 15:
				s = "FTL";
				break;
			case 16:
				s = "Aev";
				break;
			case 17:
				s = "Aod";
				break;

			case 18:
				s = "Big";
				break;
			case 19:
				s = "Sml";
				break;
			case 20:
				s = "1xS";
				break;
			case 21:
				s = "2xS";
				break;
			case 22:
				s = "3xS";
				break;
			case 23:
				s = "BBL";
				break;
			case 24:
				s = "FTL";
				break;
			case 25:
				s = "Abg";
				break;
			case 26:
				s = "Asl";
				break;
			}

			t = s + t;
			if (i < 26)
				go = false;
			i = i / 26;
			i--;
		}
		return t;
	}

}
